package com.lyun.util;

import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Component
public class MyStrUtils {

    /**
     * 将字符串按照标识符拆分成List<String>
     *
     * @param str   要拆分的字符串
     * @param regex 拆分标识符
     * @return
     */
    public List<String> strToList(String str, String regex) {
        String[] splitArray = str.split(regex);
        return Arrays.asList(splitArray);
    }

    /**
     * 将字符串按照标识符拆分成List<String>
     *
     * @param str    要拆分的字符串
     * @param regex  拆分标识符
     * @param prefix 前缀
     * @return
     */
    public List<String> strToListWithPrefix(String str, String regex, String prefix) {
        String[] parts = str.split(regex);

        List<String> resultList = new ArrayList<>();

        StringBuffer stringBuffer = new StringBuffer();

        for (String part : parts) {
            stringBuffer.setLength(0); // 重置 StringBuffer
            stringBuffer.append(prefix);
            stringBuffer.append(part);
            resultList.add(stringBuffer.toString());
        }
        return resultList;
    }
}
